package com.fancy.mobileschool.service;

import android.util.Log;

import com.fancy.mobileschool.util.GsonUtil;
import com.fancy.mobileschool.util.OKHttpUtil;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by Jackie on 2017/11/27.
 *
 */

class OKHttpBase {


    private static OkHttpClient httpClient = OKHttpUtil.getHttpClient();

    private static Gson gson = GsonUtil.getGson();

    static <T> List<T> fromJsonListByGet(String url, Class<T> clazz){

        List<T> lst = new ArrayList<>();

        try {
            Request request = new Request.Builder().url(url).build();
            Response response = httpClient.newCall(request).execute();
            String json = response.body().string();

            JsonArray array = new JsonParser().parse(json).getAsJsonArray();
            for (final JsonElement elem : array) {
                lst.add(new Gson().fromJson(elem, clazz));
            }
        } catch (Exception ignored) {

        }

        return lst;
    }

    public static <T> List<T> fromJsonListByPost(String url, RequestBody body, Class<T> clazz) {

        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();

        List<T> lst = new ArrayList<>();
        try {
            Response response = httpClient.newCall(request).execute();
            String json = response.body().string();
            JsonArray array = new JsonParser().parse(json).getAsJsonArray();
            for (final JsonElement elem : array) {
                lst.add(gson.fromJson(elem, clazz));
            }
        } catch (Exception ignored) {

        }

        return lst;
    }

    static <T> T fromJsonByGet(String url, Class<T> clazz) {
        T data = null;
        try {
            Request request = new Request.Builder().url(url).build();
            Response response = httpClient.newCall(request).execute();
            String json = response.body().string();
            data = gson.fromJson(json, clazz);
        }catch (Exception ignored) {

        }

        return data;
    }

    static <T> T fromJsonByPost(String url, RequestBody body, Class<T> clazz) {
        T data = null;
        try {
            Request request = new Request.Builder()
                    .url(url)
                    .post(body)
                    .build();

            Response response = httpClient.newCall(request).execute();
            String json = response.body().string();
            data = gson.fromJson(json, clazz);
        }catch (Exception ignored) {
            ignored.printStackTrace();
        }

        return data;
    }

    static boolean uploadFile(String url, File file) {
        RequestBody filebody = RequestBody.create(MediaType.parse("application/octet-stream"), file);
        RequestBody body = new MultipartBody.Builder()
                .addFormDataPart("file", file.getName(), filebody)
                .build();
        final Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();

        Response response = null;
        try {
            response = httpClient.newCall(request).execute();
        } catch (IOException ignored) {

        }

        return response.code() == 200;
    }


    public static String downLoadFile(String url, String desFile) {
        try {

            Request request = new Request.Builder().url(url).build();
            Response response = httpClient.newCall(request).execute();
            InputStream in = response.body().byteStream();
            File file = new File(desFile);

            if (!file.exists())
                file.createNewFile();

            OutputStream outputStream = new FileOutputStream(file);
            byte[] buffer = new byte[1024];
            int count = -1;
            while ((count = in.read(buffer)) > 0) {
                outputStream.write(buffer, 0, count);
            }

            return file.getName();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

}
